#!/bin/sh
#
# see: dh_installdeb(1)

set -e

package="doc-beaglebone-getting-started"
core_version="2019-10-31"
file_type="img"
#file_type="iso"
wfile="beaglebone-getting-started-${core_version}.${file_type}"
wfile_xz="beaglebone-getting-started-${core_version}.${file_type}.xz"

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

just_install () {
	cd /var/cache/${package}/

	if [ ! -f ${wfile} ] ; then
		unxz ${wfile_xz}
	fi

	if [ -f ${wfile} ] ; then
		ln -sf /var/cache/${package}/beaglebone-getting-started-${core_version}.${file_type} /var/local/bb_usb_mass_storage.${file_type}
	fi
}

remove_file () {
	if [ -f /var/cache/${package}/${wfile} ] ; then
		rm -f /var/cache/${package}/${wfile} || true
	fi
}

remove_dir () {
	if [ -d ${wdir} ] ; then
		rm -rf ${wdir} || true
	fi
}

remove_all() {
	wfile="beaglebone-getting-started-2015-12-04.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2015-12-04.img" ; remove_file
	wfile="beaglebone-getting-started-2016-07-18.iso.xz" ; remove_file
	wfile="beaglebone-getting-started-2016-07-18.iso" ; remove_file
	wfile="beaglebone-getting-started-2016-11-07.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2016-11-07.img" ; remove_file
	wfile="beaglebone-getting-started-2017-01-25.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2017-01-25.img" ; remove_file
	wfile="beaglebone-getting-started-2017-05-25.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2017-05-25.img" ; remove_file
	wfile="beaglebone-getting-started-2017-05-26.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2017-05-26.img" ; remove_file
	wfile="beaglebone-getting-started-2018-09-13.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2018-09-13.img" ; remove_file
	wfile="beaglebone-getting-started-2019-08-03.img.xz" ; remove_file
	wfile="beaglebone-getting-started-2019-08-03.img" ; remove_file
	wfile="beaglebone-getting-started-2019-10-31.img.xz" ; remove_file

	#Always keep the current one for quick reinstalls...
	#wfile="beaglebone-getting-started-2019-10-31.img" ; remove_file
}

case "$1" in
    configure)
	just_install
	remove_all
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

